package com.weiadmin.controller.system;

import com.weiadmin.dao.StudentDao;
import com.weiadmin.exception.GlobalException;
import com.weiadmin.model.system.UserModel;
import com.weiadmin.repository.UserRepository;
import com.weiadmin.response.RestResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述
 * 用户实体操作api
 *
 * @author: shizhongyu
 * @date: 2023年03月18日 11:26
 */
@RestController
@RequestMapping("/api/users")
public class UsersController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private StudentDao studentMapper;

    public ResponseEntity<?> allstudent(@RequestParam(value = "page", defaultValue = "0") int page,
                                        @RequestParam(value = "size", defaultValue = "10") int size) {
        Map<String, Object> map = new HashMap<>();
        RestResponse<Object> response = new RestResponse<>();

        return response.success("请求成功", map);
    }

    @GetMapping("alluser")
    public ResponseEntity<?> alluser(@RequestParam(value = "page", defaultValue = "0") int page,
                                     @RequestParam(value = "size", defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size, Sort.by("id"));

        Page<UserModel> list = userRepository.findAll(pageable);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        RestResponse<Object> response = new RestResponse<>();
        return response.success("请求成功", map);
    }

    @GetMapping("/list")
    public ResponseEntity<?> list() {
        List<UserModel> list = userRepository.findAll();
        RestResponse<List<UserModel>> response = new RestResponse<>();
        return response.success("请求成功", list);
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<?> get(@PathVariable Long id) {
        UserModel user = userRepository.findById(id)
                .orElseThrow(() -> new GlobalException("User not found"));
        RestResponse<UserModel> response = new RestResponse<>();
        return response.success("请求成功", user);
    }

    @PostMapping("/create")
    public ResponseEntity<?> create(@RequestBody UserModel user) {
        RestResponse<UserModel> response = new RestResponse<>();
        return response.success("保存成功", userRepository.save(user));
    }

    @PostMapping("/update")
    public ResponseEntity<?> update(@Valid @RequestBody UserModel user) {
        UserModel existingUser = userRepository.findById(user.getId())
                .orElseThrow(() -> new GlobalException("User not found"));
//        existingUser.setName(user.getName());
//        existingUser.setEmail(user.getEmail());
        RestResponse<UserModel> response = new RestResponse<>();
        return response.success("保存成功", userRepository.save(existingUser));
    }

    @GetMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable Long id) {

        UserModel existingUser = userRepository.findById(id)
                .orElseThrow(() -> new GlobalException("User not found"));
        userRepository.delete(existingUser);
        RestResponse<UserModel> response = new RestResponse<>();
        return response.error("删除成功", null);
    }

}
